package com.wing.invLocaltion.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.wing.auth.service.AuthService;
import com.wing.common.utils.JsonResult;
import com.wing.invLocaltion.entity.request.InvLocationCreateRequest;
import com.wing.invLocaltion.entity.request.InvLocationQueryRequest;
import com.wing.invLocaltion.entity.response.InvLocationQueryResponse;
import com.wing.redis.utils.redis.RedisHelperImpl;
import com.wing.set.entity.ChanjetSet;
import com.wing.set.service.ChanjetSetService;
import com.wing.util.ConstantsUtil;
import com.wing.util.HttpsUtils;
import com.wing.warehouse.entity.response.WarehouseQueryResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 货位管理
 */
@Service
@Slf4j
public class InvLocationServiceImpl implements InvLocationService {

    @Resource
    RedisHelperImpl redisHelper;

    @Resource
    AuthService authService;

    @Resource
    ChanjetSetService chanjetSetService;

    @Override
    public JsonResult invLocationCreate(InvLocationCreateRequest invLocationCreateRequest) {
        ChanjetSet chanjetSet = chanjetSetService.getById(1L);

        Map<String, String> headers = new HashMap<>();
        headers.put("appKey", chanjetSet.getAppKey());
        headers.put("appSecret", chanjetSet.getAppSecret());
        if(redisHelper.getValue("chanjetToken") == null){
            authService.getToken();
        }
        headers.put("openToken", redisHelper.getValue("chanjetToken").toString());

        Map<String, Object> param = new HashMap<>();
        param.put("dto", invLocationCreateRequest);
        System.out.println("货位创建1111：" + JSON.toJSONString(param));
        String result = HttpsUtils.httpsPostByJson(ConstantsUtil.INVLOCATION_CREATE, headers, JSON.toJSONString(param));
        System.out.println("货位创建：" + result);
        //根据返回信息解析-待完成

        return JsonResult.success();
    }

    @Override
    public List<InvLocationQueryResponse> invLocationQuery(InvLocationQueryRequest invLocationQueryRequest) {
        ChanjetSet chanjetSet = chanjetSetService.getById(1L);

        Map<String, String> headers = new HashMap<>();
        headers.put("appKey", chanjetSet.getAppKey());
        headers.put("appSecret", chanjetSet.getAppSecret());
        if(redisHelper.getValue("chanjetToken") == null){
            authService.getToken();
        }
        headers.put("openToken", redisHelper.getValue("chanjetToken").toString());

        invLocationQueryRequest.setSelectFields("Code,Name,Shorthand,Warehouse.Code,Warehouse.Name,Disabled,Memo,Ts");
        Map<String, Object> param = new HashMap<>();
        param.put("param", invLocationQueryRequest);

        String result = HttpsUtils.httpsPostByJson(ConstantsUtil.INVLOCATION_QUERY, headers, JSON.toJSONString(param));

        List<InvLocationQueryResponse> list=new ArrayList<>();
        // 解析JSON字符串为JSONArray
        JSONArray jsonArray = JSON.parseArray(result);
        // 遍历JSONArray中的每个JSONObject
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            // 获取顶层字段的值
            String code = jsonObject.getString("Code");
            String name = jsonObject.getString("Name");
            String shorthand = jsonObject.getString("Shorthand");
            boolean disabled = jsonObject.getBoolean("Disabled");
            String memo = jsonObject.getString("Memo");
            String ts = jsonObject.getString("Ts");

            // 对于嵌套的对象，比如Warehouse，可以这样获取
            String warehouseCode = jsonObject.getString("Warehouse.Code");
            String warehouseName = jsonObject.getString("Warehouse.Name");

            InvLocationQueryResponse invLocationQueryResponse = new InvLocationQueryResponse();
            invLocationQueryResponse.setCode(code);
            invLocationQueryResponse.setName(name);
            invLocationQueryResponse.setShorthand(shorthand);
            invLocationQueryResponse.setDisabled(disabled);
            invLocationQueryResponse.setMemo(memo);
            invLocationQueryResponse.setTs(ts);
            invLocationQueryResponse.setWarehouseCode(warehouseCode);
            invLocationQueryResponse.setWarehouseName(warehouseName);
            list.add(invLocationQueryResponse);
        }
         return list;
    }
}
